package com.ugoodtech.mdcc.web.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.domain.QQuestionAnswerOption;
import com.ugoodtech.mdcc.core.domain.Question;
import com.ugoodtech.mdcc.core.domain.QuestionAnswerOption;
import com.ugoodtech.mdcc.core.repository.QuestionAnswerOptionRepository;
import com.ugoodtech.mdcc.core.repository.QuestionRepository;
import com.ugoodtech.mdcc.web.service.QuestionAnswerOptionService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class QuestionAnswerOptionServiceImpl implements QuestionAnswerOptionService {

    @Autowired
    private QuestionAnswerOptionRepository questionAnswerOptionRepository;

    @Autowired
    private QuestionRepository questionRepository;

    @Override
    public Page<QuestionAnswerOption> queryOption(Long questionId, Pageable pageable) {
        QQuestionAnswerOption qQuestionAnswerOption = QQuestionAnswerOption.questionAnswerOption;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionAnswerOption.deleted.eq(false));
        if (null!=questionId) {
            builder.and(qQuestionAnswerOption.question.id.eq(questionId));
        }
        Page<QuestionAnswerOption> questionAnswerOptions = questionAnswerOptionRepository.findAll(builder,pageable);
        return questionAnswerOptions;
    }


    @Override
    public QuestionAnswerOption createAnswer(@ApiParam("问题Id")  Long questionId,
                                             @ApiParam("选项文字")  String text,
                                             @ApiParam("允许填空")  boolean addText,
                                             @ApiParam("结果文字")  String resultText,
                                             @ApiParam("建议文字")  String suggestText) {
        Question question=questionRepository.findOne(questionId);
        QuestionAnswerOption questionAnswerOption=new QuestionAnswerOption();
        questionAnswerOption.setQuestion(question);
        questionAnswerOption.setName(text);
        questionAnswerOption.setAddText(addText);
        questionAnswerOption.setDescription(resultText);
        questionAnswerOption.setExtra(suggestText);
        questionAnswerOptionRepository.save(questionAnswerOption);
        return questionAnswerOption;
    }
}
